$(document).ready(function()
    {
        var angsuran;
        var piutang;
        
        $('.date-picker').datepicker( {
            changeYear: true,
            changeMonth: true,
            showWeek: true,
            showButtonPanel: true,
            dateFormat: 'yy-mm',
            onClose: function(dateText, inst) {
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
                $(this).datepicker('setDate', new Date(year, month, 1));
            }
        });

        $.edit_angsuran = function(angsuran_ke)
        {
            if(jenis_kredit == 2)
            {
                var temp_piutang = (angsuran_ke == 1) ? jumlah_pinjaman : $("#piutang_"+id_pinjaman+"_"+(angsuran_ke - 1)).val();
                var sisa_bulan = lama_pinjaman - angsuran_ke;

                var new_angsuran = $("#angsuran_"+ id_pinjaman +"_"+ angsuran_ke).val() - r_bunga;
                temp_piutang -= new_angsuran;

                $("#piutang_"+ id_pinjaman +"_"+ angsuran_ke).val(temp_piutang);
                for (var i = angsuran_ke + 1; i <= lama_pinjaman; i++) {
                    new_angsuran = $("#angsuran_"+ id_pinjaman +"_"+ i).val() - r_bunga;
                    temp_piutang -= new_angsuran;
                    $("#piutang_"+ id_pinjaman +"_"+ i).val(temp_piutang);
                }
            }
            if(jenis_kredit == 3)
            {

            }
        }

        $.notif_edit_angsuran = function(data)
        {
            for(var i = 0; i < data.data.length; i--)
            {
                var current = data.data[i];
                if(current.ANGSURAN_KE >= angsuran)
                    $("#edit_skema_table tbody").append("<tr>\n\
                                    <td>" + current.ID_PINJAMAN + "</td>\n\
                                    <td>" + current.ANGSURAN_KE + "</td>\n\
                                    <td>" + current.CICILAN_POKOK + "</td>\n\
                                    <td>" + current.BUNGA + "</td>\n\
                                    <td> <input style='float:none; margin: 0 0 0 0; width:100px;' type='text' id='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' name='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' value='" + current.ANGSURAN + "' onchange='$.edit_angsuran("+current.ANGSURAN_KE+")'/></td>\n\
                                    <td> <input style='float:none; margin: 0 0 0 0; width:100px;' type='text' id='piutang_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' name='piutang_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' readonly value='" + current.PIUTANG + "'/></td>\n\
                                    </tr>");
                else
                    $("#edit_skema_table tbody").append("<tr>\n\
                                    <td>" + current.ID_PINJAMAN + "</td>\n\
                                    <td>" + current.ANGSURAN_KE + "</td>\n\
                                    <td>" + current.CICILAN_POKOK + "</td>\n\
                                    <td>" + current.BUNGA + "</td>\n\
                                    <td> <input disabled style='float:none; margin: 0 0 0 0; width:100px;' type='text' id='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' name='angsuran_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' value='" + current.ANGSURAN + "'/></td>\n\
                                    <td> <input disabled style='float:none; margin: 0 0 0 0; width:100px;' type='text' id='piutang_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' name='piutang_"+ current.ID_PINJAMAN +"_"+ current.ANGSURAN_KE +"' readonly value='" + current.PIUTANG + "'/></td>\n\
                                    </tr>");
            }
        }

        $.edit_angsuran = function(id_pinjaman, _angsuran)
        {
            angsuran = _angsuran;
            $('#edit_skema').dialog('open');
            $('#edit_skema_table tbody').html('');

            $.data_ajax('br-03/potongan/id/' + id_pinjaman, null, $.notif_edit_angsuran);
        }

        $.pelunasan_angsuran = function(id_pinjaman, _angsuran, _piutang)
        {
            angsuran = _angsuran;
            piutang = _piutang;
            $("#ID_PINJAMAN").val(id_pinjaman);
            $("#ANGSURAN_KE").val(angsuran);
            $('#dialog-pelunasan').dialog('open');
            $.data_ajax('br-02/pinjaman/get/' + id_pinjaman, null, $.edit_pelunasan_angsuran);
        }

        $.edit_pelunasan_angsuran = function(data)
        {
            for(var i = data.data.length - 1; i >= 0 ; i--)
            {
                var current = data.data[i];
                $("#ID_ANGGOTA").val(current.ID_ANGGOTA);
                if (current.ID_JENIS_KREDIT == 1)
                $("#JENIS_PINJAMAN").val('Syariah');
                    else if (current.ID_JENIS_KREDIT == 2)
                        $("#JENIS_PINJAMAN").val('Cicilan Flat');
                            else if (current.ID_JENIS_KREDIT == 3)
                                $("#JENIS_PINJAMAN").val('Cicilan Menurun');

              // $("#PIUTANG_SISA").val(piutang);

               var denda = 0;
               var pinjaman = current.JUMLAH_PINJAMAN;
               var bunga = current.BUNGA;
               var lama = current.LAMA_PINJAMAN;
               var cicilan = pinjaman/lama;
               for(var j=1; j<=angsuran; j++){
                   denda += pinjaman * (bunga/100);
                   pinjaman = pinjaman - cicilan;
               }
               $("#DENDA").val(denda);
               $("#BUNGA").val(denda);
               $("#TOTAL").val(denda+piutang);
               $("#PIUTANG").val(denda+piutang);
            }
        }

        $.notif_angsuran = function(data)
        {
            $("#SKEMA_POTONGAN tbody").html('');
            for(var i = 0; i < data.data.length; i++)
            {
                var current = data.data[i];
                $("#SKEMA_POTONGAN tbody").append("<tr>\n\
                                    <td>" + current.ID_PINJAMAN + "</td>\n\
                                    <td>" + current.ANGSURAN_KE + "</td>\n\
                                    <td>" + current.CICILAN_POKOK + "</td>\n\
                                    <td>" + current.BUNGA + "</td>\n\
                                    <td>" + current.ANGSURAN + "</td>\n\
                                    <td>" + current.PIUTANG + "</td>\n\
                                    <td><div style='text-align:center;'> <ul class='action-buttons clearfix'><li><a class='button button-gray no-text' href='#' onClick='$.edit_angsuran(" + current.ID_PINJAMAN + ", "+ current.ANGSURAN_KE + ")'> <span class='pencil'></span></a></li><li><a class='button button-gray no-text' href='#' onClick='$.pelunasan_angsuran(" + current.ID_PINJAMAN + ", "+ current.ANGSURAN_KE + "," + current.PIUTANG + ")'> <span class='accept'></span></a></li></ul></div> \n\ </td>\n\
                                    </tr>");
            }
        }

        $.get_angsuran = function()
        {
            if($("#TGL_ANGSURAN").val() != "")
                $.data_ajax('br-03/potongan/date/' + $("#TGL_ANGSURAN").val(), null, $.notif_angsuran);
        }

        $.submit_angsuran = function()
        {
            $.call_ajax('br-03/potongan/', '#form-pelunasan', null);
        }

        $('#edit_skema').dialog({
            title: 'Data info_simpanan',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $('#dialog-pelunasan').dialog({
            title: 'Form Pelunasan',
            width: '950px',
            autoOpen:false,
            modal:true,
            resizable:false
        });

        $.submit_angsuran = function()
        {
        }

        $("#FORM_BUAT_POTONGAN").submit(function() {
            return false;
        });

        $("#form-pelunasan").submit(function() {
            return false;
        });
        
        $("#TGL_ANGSURAN").blur($.get_angsuran);

        $("#ADD").click($.submit_angsuran);

        $("#ADD_LUNAS").hide();
        //$("#ADD_LUNAS").click($.submit_pelunasan);

    });